package com.naja.auth2clientfirst.web;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.core.ClaimAccessor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Map;

/**
 * @auther wangjianying
 * @date 2024/01/09 14:54
 */
@Controller
@RequestMapping("/workflow")
public class WorkflowController {

    @GetMapping(value = "/test1")
    @PreAuthorize("hasAnyAuthority('test1')")
    public String test1(Model model) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        ClaimAccessor principal = (ClaimAccessor) authentication.getPrincipal();
        Map<String, Object> claims = principal.getClaims();
        model.addAttribute("test1", JSONObject.toJSONString(claims));
        return "index";
    }

}
